//
//  BindCardViewController.m
//  at_xiaoge_client
//
//  Created by Linyu Niu on 21/03/2018.
//  Copyright © 2018 Linyu Niu. All rights reserved.
//

#import "BindCardViewController.h"
#import "UIPickerBankTypeProtocol.h"
#import "CustomerUIPickerView.h"
#import "UIPickerProvinceCityProtocol.h"

#import "XLBankScanViewController.h"


@interface BindCardViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
{
    NSMutableDictionary * bindData;
    UITableView * bindTableView;
    NSArray * bankTypeList;
    NSArray * provinceList;//省市
    UIPickerBankTypeProtocol * bankTypeDataSource;
    UIPickerProvinceCityProtocol * provinceDataSource;
}
@end

@implementation BindCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    bindData = [[NSMutableDictionary alloc]init];
    __weak BindCardViewController * weakSelf = self;
    UIView * navView = [[CTMediator sharedInstance] navgationHeadViewInTargetView:self.view params:[NSDictionary dictionaryWithObjectsAndKeys:@"绑定银行卡",@"title", nil] leftAction:^(UIButton * sender){
        [weakSelf.navigationController popViewControllerAnimated:YES];
    } rightAction:nil];
    
    bindTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, navView.frame.origin.y + navView.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - navView.frame.size.height - navView.frame.origin.y)];
    bindTableView.delegate = self;
    bindTableView.dataSource = self;
    bindTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:bindTableView];
    [self getBankType];
}


-(void)getBankType{
    
    
}
-(void)scanCard{
    XLBankScanViewController * controller = [[XLBankScanViewController alloc]init];
    controller.finishBlock = ^(XLScanResultModel *model) {
        if(model.bankNumber){
            [bindData setObject:model.bankNumber forKey:@"cardNO"];
            [bindTableView reloadData];
        }
    };
    
    [self.navigationController pushViewController:controller animated:YES];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    view.frame = CGRectMake(0, 0, self.view.frame.size.width, 40);
    view.backgroundColor = [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:0.04];
    
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);
    label.text = @"请绑定持卡人本人的储蓄卡";
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:14];
    label.textColor = [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:0.5];
    [view addSubview:label];
    return view;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ElementTableViewCell * cell = [[ElementTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    if ([indexPath row] <= 3) {
        cell.cellHeight = 50;
        UILabel *label = [[UILabel alloc] init];
        label.frame = CGRectMake(20, 0, 100, cell.cellHeight);
        
        label.font = [UIFont systemFontOfSize:14];
        label.textColor = UIColorFromRGB(0x333333);
        [cell addSubview:label];
        if ([indexPath row] == 0) {
            label.text = @"持卡人";
        }else if([indexPath row] == 1){
            label.text = @"银行卡号";
        }else if([indexPath row] == 2){
            label.text = @"银行类别";
        }else if([indexPath row] == 3){
            label.text = @"开卡城市";
        }
        
        //
        UIView * line1 = [[UIView alloc]initWithFrame:CGRectMake(0, cell.cellHeight - 1.0, tableView.frame.size.width, 1.0)];
        line1.backgroundColor = UIColorFromRGBWithAlpha(0xE9E9E9, 1.0);
        [cell addSubview:line1];
        
        UIView * vLine = [[UIView alloc]initWithFrame:CGRectMake(label.frame.origin.x + label.frame.size.width + 5,8,1.0, cell.cellHeight - 16)];
        vLine.backgroundColor = UIColorFromRGBWithAlpha(0xE9E9E9, 1.0);
        [cell addSubview:vLine];
        if ([indexPath row] == 0 || [indexPath row] == 1) {
            UITextField * field = [[UITextField alloc]initWithFrame:CGRectMake(vLine.frame.origin.x + vLine.frame.size.width + 5, vLine.frame.origin.y, tableView.frame.size.width - vLine.frame.size.width - vLine.frame.origin.x - 10, vLine.frame.size.height)];
            field.tag = [indexPath row];
            field.delegate = self;
            field.textColor = UIColorFromRGB(0xBCBCBC);
            field.font = [UIFont systemFontOfSize:14];
            [cell addSubview:field];
            if ([indexPath row] == 0) {
                field.placeholder = @"请输入持卡人姓名";
                field.text = [bindData objectForKey:@"cardholder"];
            }else if([indexPath row] == 1){
                field.placeholder = @"请输银行卡号";
                field.text = [bindData objectForKey:@"cardNO"];
                field.keyboardType = UIKeyboardTypeNumberPad;
                UIButton * scanButton = [UIButton buttonWithType:UIButtonTypeCustom];
                [scanButton setImage:[UIImage imageNamed:@"qr_icon"] forState:UIControlStateNormal];
                scanButton.frame = CGRectMake(tableView.frame.size.width - 40, 0, 30, cell.cellHeight);
                [scanButton addTarget:self action:@selector(scanCard) forControlEvents:UIControlEventTouchUpInside];
                [cell addSubview:scanButton];
            }
        }else{
            UILabel * nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(vLine.frame.origin.x + vLine.frame.size.width + 5, vLine.frame.origin.y, tableView.frame.size.width - vLine.frame.size.width - vLine.frame.origin.x - 10, vLine.frame.size.height)];
            nameLabel.textColor = UIColorFromRGB(0xBCBCBC);
            nameLabel.font = [UIFont systemFontOfSize:14];
            [cell addSubview:nameLabel];
            if ([indexPath row] == 2) {
               nameLabel.text = [bindData objectForKey:@"bankName"];
            }else{
                nameLabel.text = [bindData objectForKey:@"cardCity"];
            }
        }
        
    }else{
        UIButton * saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [saveButton addTarget:self action:@selector(bindCard) forControlEvents:UIControlEventTouchUpInside];
        saveButton.layer.cornerRadius = 13;
        saveButton.frame = CGRectMake(44, 30, tableView.frame.size.width - 88, 42);
        saveButton.backgroundColor = [UIColor colorWithRed:32.0/255.0 green:198.0/255.0 blue:122.0/255.0 alpha:1.0];
        saveButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [saveButton addTarget:self action:@selector(bindCard) forControlEvents:UIControlEventTouchUpInside];
        [saveButton setTitle:@"绑定银行卡" forState:UIControlStateNormal];
        [cell addSubview:saveButton];
        cell.cellHeight = saveButton.frame.size.height + saveButton.frame.origin.y + 10;
    }
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([indexPath row] == 2) {
        
        if (!bankTypeDataSource) {
            bankTypeDataSource = [[UIPickerBankTypeProtocol alloc]init];
        }
        bankTypeDataSource.typeList = bankTypeList;
        
        [[CTMediator sharedInstance] showBankTypePicker:self.view delegate:bankTypeDataSource block:^(void){
            //
            if (bankTypeDataSource.selectedData) {
                [bindData setObject:[bankTypeDataSource.selectedData objectForKey:@"bankName"] forKey:@"bankName"];
                [bindData setObject:[bankTypeDataSource.selectedData objectForKey:@"bankId"] forKey:@"bankId"];
                [bindTableView reloadData];
            }
        }];
        
    }else if([indexPath row] == 3){
        
        
        if (!provinceDataSource) {
            provinceDataSource = [[UIPickerProvinceCityProtocol alloc]init];
        }
        [provinceDataSource setData:provinceList];
        [[CTMediator sharedInstance] showBankTypePicker:self.view delegate:provinceDataSource block:^(void){
            if (provinceDataSource.selectedCity) {
                [bindData setObject:[provinceDataSource.selectedCity objectForKey:@"name"] forKey:@"cardCity"];
                [bindData setObject:[provinceDataSource.selectedCity objectForKey:@"cityId"] forKey:@"cardCityId"];
                [bindTableView reloadData];
            }
        }];
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ElementTableViewCell * cell = (ElementTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.cellHeight;
}
-(void)bindCard{
    [self.view endEditing:YES];
    NSString * cardholder = [bindData objectForKey:@"cardholder"];
    NSString * cardNO = [bindData objectForKey:@"cardNO"];
    NSString * bankName = [bindData objectForKey:@"bankId"];
    NSString * cardCity = [bindData objectForKey:@"cardCityId"];
    if (cardholder
        && ![@"" isEqualToString:cardholder]
        && cardNO
        && ![@"" isEqualToString:cardNO]
        && bankName
        && ![@"" isEqualToString:bankName]
        && cardCity
        && ![@"" isEqualToString:cardCity]) {
        [self startLoadingView:self.view];
        
        __weak BindCardViewController * weakSelf = self;
        
        
    }
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    
    NSString * text = [textField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    if ([text length]) {
        if (textField.tag == 0) {
            [bindData setObject:textField.text forKey:@"cardholder"];
        }else if(textField.tag == 1){
            [bindData setObject:textField.text forKey:@"cardNO"];
        }else if(textField.tag == 2){
            [bindData setObject:textField.text forKey:@"bankName"];
        }else if(textField.tag == 3){
            [bindData setObject:textField.text forKey:@"cardCity"];
        }
    }
}


@end
